package com.conv.shop.business.service;

import com.conv.shop.business.domain.Product;
import com.conv.shop.business.domain.ShoppingCart;
import com.conv.shop.business.domain.ShoppingCartExample;
import com.conv.shop.business.mapper.ProductMapper;
import com.conv.shop.business.mapper.ShoppingCartMapper;
import com.conv.shop.business.req.CartUpdateReq;
import com.conv.shop.business.req.CartInfo;
import com.conv.shop.business.resp.CartResp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CartService {
    @Resource
    ShoppingCartMapper shoppingCartMapper;

    @Resource
    ProductMapper productMapper;

    /**
     * 添加购物车
     * @param cart
     * @return boolean
     */
    public boolean cartAdd(CartInfo cart) {
        ShoppingCartExample example = new ShoppingCartExample();
        ShoppingCart shoppingCart = new ShoppingCart();
        example.createCriteria().andProductIdEqualTo(cart.getProductId());
        List<ShoppingCart> firstCart = shoppingCartMapper.selectByExample(example);
        if(!firstCart.isEmpty()){
            shoppingCart.setShoppingCartId(firstCart.get(0).getShoppingCartId());
            shoppingCart.setNumber(firstCart.get(0).getNumber()+cart.getNumber());
            shoppingCart.setLastUpdateTime(cart.getLastUpdateTime());
            shoppingCart.setConsumerId(cart.getConsumerId());
            shoppingCart.setAddToCartTime(firstCart.get(0).getAddToCartTime());
            shoppingCart.setProductId(cart.getProductId());
            shoppingCart.setSelected(firstCart.get(0).getSelected());
            shoppingCartMapper.updateByPrimaryKeySelective(shoppingCart);
            return true;
        }
        shoppingCart.setAddToCartTime(new Date(System.currentTimeMillis()));
        shoppingCart.setLastUpdateTime(cart.getLastUpdateTime());
        shoppingCart.setConsumerId(cart.getConsumerId());
        shoppingCart.setNumber(cart.getNumber());
        shoppingCart.setProductId(cart.getProductId());
        shoppingCart.setSelected(cart.getSelected());
        shoppingCartMapper.insert(shoppingCart);
        return true;

    }

    /**
     * 购物车删除
     * @param cart
     * @return boolean
     */
    public boolean cartDelete(ShoppingCart cart ) {
        return shoppingCartMapper.deleteByPrimaryKey(cart.getShoppingCartId()) != 0;
    }

    /**
     * 购物车更新
     * @param cart
     * @return boolean
     */
    public boolean cartUpdate(CartUpdateReq cart) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setShoppingCartId(cart.getShoppingCartId());
        shoppingCart.setNumber(cart.getNumber());
        shoppingCart.setLastUpdateTime(new Date(System.currentTimeMillis()));
        shoppingCart.setNumber(cart.getNumber());
        return shoppingCartMapper.updateByPrimaryKeySelective(shoppingCart) != 0;
    }

    /**
     * 获取购物车列表
     * @param consumerId
     * @return List<CartResp>
     */
    public List<CartResp> getCartList(Integer consumerId) {
        ShoppingCartExample shoppingCartExample = new ShoppingCartExample();
        shoppingCartExample.createCriteria().andConsumerIdEqualTo(consumerId);
        List<ShoppingCart> shoppingCartLists =  shoppingCartMapper.selectByExample(shoppingCartExample);
        List<CartResp> cartRespList = new java.util.ArrayList<>();
        for(ShoppingCart shoppingCart : shoppingCartLists){
            Product product = productMapper.selectByPrimaryKey(shoppingCart.getProductId());
            CartResp cartResp = getCartResp(shoppingCart, product);
            cartRespList.add(cartResp);
        }
        return cartRespList;
    }

    /**
     * 查询购物车和商品
     * @param shoppingCart
     * @param product
     * @return CartResp
     */
    private static CartResp getCartResp(ShoppingCart shoppingCart, Product product) {
        CartResp cartResp = new CartResp();
        cartResp.setStoreId(product.getStoreId());
        cartResp.setSelected(shoppingCart.getSelected());
        cartResp.setNumber(shoppingCart.getNumber());
        cartResp.setShoppingCartId(shoppingCart.getShoppingCartId());
        cartResp.setProductId(shoppingCart.getProductId());
        cartResp.setProductName(product.getProductName());
        cartResp.setAddToCartTime(shoppingCart.getAddToCartTime());
        cartResp.setImageUrl(product.getImageUrl());
        cartResp.setPrice(product.getPrice());
        return cartResp;
    }

    /**
     * 清空购物车
     * @param consumerId
     * @return boolean
     */
    public boolean cartClear(Integer consumerId) {
        ShoppingCartExample shoppingCartExample = new ShoppingCartExample();
        shoppingCartExample.createCriteria().andConsumerIdEqualTo(consumerId);
        return shoppingCartMapper.deleteByExample(shoppingCartExample) != 0;
    }
}
